
// GarminTCXHandlerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GarminTCXHandler.h"
#include "GarminTCXHandlerDlg.h"
#include "tinyxml.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CGarminTCXHandlerDlg dialog




CGarminTCXHandlerDlg::CGarminTCXHandlerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGarminTCXHandlerDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_dfDistanceMeters = 0.0;
}

void CGarminTCXHandlerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CGarminTCXHandlerDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BTN_SET, &CGarminTCXHandlerDlg::OnBnClickedBtnSet)
END_MESSAGE_MAP()


// CGarminTCXHandlerDlg message handlers

BOOL CGarminTCXHandlerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGarminTCXHandlerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CGarminTCXHandlerDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGarminTCXHandlerDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CGarminTCXHandlerDlg::OnBnClickedBtnSet()
{
	TiXmlDocument doc( "d:\\SalitaMDonato_course.tcx" );
	BOOL loadOkay = doc.LoadFile();
	if (loadOkay)
	{
		TiXmlHandle hDoc(&doc);
		TiXmlElement* pElem;
		TiXmlHandle hRoot(0);
		TiXmlHandle hTrack(0);

		pElem=hDoc.FirstChildElement().Element();
		// should always have a valid root but handle gracefully if it does
		if (!pElem) return;
		//m_name=pElem->Value();

		// save this for later
		hRoot=TiXmlHandle(pElem);

	/*	TiXmlElement* pTrackElement=hRoot.FirstChild( "Courses" ).FirstChild("Course").FirstChild("Track").Element();
		TiXmlNode *pTrackNode = hRoot.FirstChild( "Courses" ).FirstChild("Course").FirstChild("Track").Node();

		for( pTrackElement; pTrackElement; pTrackElement=pTrackElement->NextSiblingElement())
		{
			TiXmlElement* pTrackPointElement = pTrackElement->FirstChildElement("Trackpoint");
			int u=0;
			if(pTrackPointElement)
			{
				TiXmlElement* pDistanceMeters = pTrackPointElement->FirstChildElement("DistanceMeters");
				if(pDistanceMeters)
				{

					const char *pText=pDistanceMeters->GetText();
					const char *pValue=pDistanceMeters->Value();
					double dfDistanceMeters = 0.0;
					if(pDistanceMeters->FirstAttribute()->QueryDoubleValue(&dfDistanceMeters) == TIXML_SUCCESS)
					{
						if(dfDistanceMeters > 1)
						{
							dfDistanceMeters -= 1; 
							pDistanceMeters->FirstAttribute()->SetDoubleValue(dfDistanceMeters);
						}
					}
				}
			}

		}*/

		TiXmlElement* pCourseElement=hRoot.FirstChild( "Courses" ).FirstChild("Course").Element();
		TiXmlNode *pCourseNode = hRoot.FirstChild( "Courses" ).FirstChild("Course").Node();

		for( pCourseElement; pCourseElement; pCourseElement=pCourseElement->NextSiblingElement())
		{
			TiXmlElement* pTrackElement = pCourseElement->FirstChildElement();

			while(pTrackElement)
			{
				if(strcmp(pTrackElement->Value(), "Track") == 0)
				{
					TiXmlElement* pTrackPointElement = pTrackElement->FirstChildElement();
					while(pTrackPointElement)
					{
						if(strcmp(pTrackPointElement->Value(), "Trackpoint") == 0)
						{
							TiXmlElement* pDistanceMeters = pTrackPointElement->FirstChildElement("DistanceMeters");
							
							if(pDistanceMeters)
							{

								double dfCurrentDistanceMeters = 0.0;
								const char *pText=pDistanceMeters->GetText();
								const char *pValue=pDistanceMeters->Value();
								if(pText)
								{
									dfCurrentDistanceMeters = atof(pText);
									if(m_dfDistanceMeters == 0)
									{
										m_dfDistanceMeters = atof(pText);
									}

									dfCurrentDistanceMeters = dfCurrentDistanceMeters - m_dfDistanceMeters;
								}
								
								if(m_dfDistanceMeters)
								{

									char szNewDistance[256];
									sprintf_s(szNewDistance, 256, "%13.7f", dfCurrentDistanceMeters);
									pTrackPointElement->RemoveChild(pDistanceMeters);
									pDistanceMeters = new TiXmlElement( "DistanceMeters" );
									TiXmlText * text = new TiXmlText( szNewDistance );
									pDistanceMeters->LinkEndChild( text );
									//pTrackPointElement->LinkEndChild(pDistanceMeters);
									pTrackPointElement->InsertAfterChild(pTrackPointElement->FirstChild("AltitudeMeters"), *pDistanceMeters);

									delete pDistanceMeters;
								}
							}
						}
						pTrackPointElement = pTrackPointElement->NextSiblingElement();
					}
				}
				pTrackElement = pTrackElement->NextSiblingElement();
			}

		}

		doc.SaveFile("d:\\SalitaMDonato_course_NEW.tcx");
		//printf("\n%s:\n", pFilename);
	//	dump_to_stdout( &doc ); // defined later in the tutorial
	}
	else
	{
		//printf("Failed to load file \"%s\"\n", pFilename);
	}
}
